<?php
/*
 * @Description:
 * @Author: 橘子味的心
 * @Date: 2020-12-18 21:51:50
 * @LastEditTime: 2021-01-26 17:25:02
 * @LastEditors: 橘子味的心
 * @Reference:
 */
namespace app\system\controller;

use app\common\tool\Upload as ToolUpload;
use think\facade\Db;

class Upload extends Base
{
    /**
     * @title   文件上传
     * @desc    文件上传
     * @author  橘子味的心
     * @version 1.0
     * @method  POST
     * @param   file file &nbsp; 文件 YES
     * @return  int code &nbsp; 返回参数 200/100
     * @return  string message &nbsp; 返回信息 successful
     * @return  array data &nbsp; 返回数据 successful
     */
    public function uploadfile()
    {
        if (request()->isPost()) {
            $files = request()->file();
            $paths = input('path');
            $data['group_id'] = input('group_id');
            $res = ToolUpload::file($files, $paths, $data);
            $res['code'] = 200;
            echo json_encode($res, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
            exit;
        }
    }
    /**
     *  分片上传文件
     */
    public function multipart_upload()
    {
        if (request()->isPost()) {
            $files = request()->file('file');
            $fileType = input('fileType');
            $fileName = input('fileName');
            $fileSize = input('fileSize');
            $fileExt = input('fileExt');
            $totalPage = input('totalPage');
            $md5 = input('md5');
            $is_md5 = input('is_md5', 'false');
            $page = input('page');
            $path = explode('/', $fileType)[0] . '/';
            $uploaddir = root_path('public') . 'upload/' . $path;
            if (!is_dir($uploaddir)) {
                mkdir($uploaddir, 0777, true);
            }
            $paths = md5($fileName) . '.' . $fileExt;
            $status = 1;
            if (!empty($is_md5) && empty($md5)) {
                $res = ['status' => 2, 'msg' => "当前文件MD5为空！"];
                return $res;
            }
            //上传文件要保存的路径
            $fname = $uploaddir . $paths;
            $data = file_get_contents($files->getPathname());
            if ($page == 1) {
                file_put_contents($fname, $data);
                if ($is_md5 != 'false') {
                    if (Db::name('attachment')->where('md5', $md5)->count()) {
                        $res = ['status' => 2, 'msg' => "当前文件已经上传过了！"];
                        return $res;
                    }
                }
                $add['url'] = $path . $paths;
                $add['name'] = $fileName;
                $add['storage'] = '0';
                $add['filesize'] = $fileSize;
                $add['mimetype'] = $fileType;
                $add['md5'] = $md5;
                $add['file_id'] = createNoncestr();
                $add['is_fp'] = 1;
                $add['create_time'] = time();
                $res['status'] = $status;
                if ($totalPage == $page) {
                    $path = explode('/', $fileType)[0] . '/' . date('Ymd') . '/';
                    $uploaddir = root_path('public') . 'upload/' . $path;
                    if (!is_dir($uploaddir)) {
                        mkdir($uploaddir, 0777, true);
                    }
                    $paths = md5(microtime(true)) . '.' . $fileExt;
                    rename($fname, $uploaddir . $paths);
                    $res = ['status' => 2, 'msg' => "上传成功!", 'url' => $path . $paths];
                    $add['url'] = $path . $paths;
                    $add['sha1'] = $files->hash('sha1');
                    $add['fp_page'] = $page;
                    $add['fp_totalPage'] = $totalPage;
                    $add['fp_status'] = 1;
                    $add['update_time'] = time();
                }
                Db::name('attachment')->insert($add);
                return $res;
            } else {
                //其余文件追加到文件末尾
                file_put_contents($fname, $data, FILE_APPEND);
            }
            $res = ['status' => $status];
            //最后一片文件
            if ($totalPage == $page) {
                $status = 2;
                $path = explode('/', $fileType)[0] . '/' . date('Ymd') . '/';
                $uploaddir = root_path('public') . 'upload/' . $path;
                if (!is_dir($uploaddir)) {
                    mkdir($uploaddir, 0777, true);
                }
                $paths = md5(microtime(true)) . '.' . $fileExt;
                rename($fname, $uploaddir . $paths);
                $res = ['status' => $status, 'msg' => "上传成功!", 'url' => $path . $paths];
                $add['url'] = $path . $paths;
                $add['fp_status'] = 1;
            } else {
                $add['fp_page'] = $page;
                $add['fp_totalPage'] = $totalPage;
                $add['update_time'] = time();
            }
            Db::name('attachment')->where('md5', $md5)->update($add);
            //返回上传状态
            return $res;
        }
    }
}
